<?php
class showController extends commonController{
//		显示首页
	public function index(){
//		加载面包屑导航		
		$fatherData = $this->navigation();
		$asid = $_GET['asid'];
		//提问信息查询
		$content=M('hd_category')->query("SELECT * FROM hd_ask WHERE asid={$asid} ");
		$content = current($content);
		$this->assign('content', $content);
//		查询用户信息
		$uid = $content['uid'];
		$userinfo=M('hd_user')->query("SELECT * FROM hd_user WHERE uid={$uid} ");
		$userinfo = current($userinfo); 
		$this->assign('userinfo', $userinfo);
		//回答信息查询
		$anserContent=M('hd_answer')->query("SELECT * FROM hd_answer WHERE asid='{$asid}' ");
		$this->assign('anserContent',$anserContent);
		//回答总条数
		$anserCount=M('hd_answer')->query("SELECT COUNT(*) FROM hd_answer WHERE asid='{$asid}' ");
		$anserCount = current($anserCount);
		$anserCount =  $anserCount['COUNT(*)']; //回答总条数
		$this->assign('anserCount', $anserCount);
//		查询采纳数据
		$asid = $_GET['asid'];
		$lastContent = M('hd_answer')->query(" SELECT *  FROM hd_answer WHERE asid='{$asid}' AND  accept = 1 ");
		$lastContent = current($lastContent);
		$lastContentUid = $lastContent['uid'];
		$lastContentUserInfo = M('hd_user')->query("SELECT * FROM hd_user WHERE uid= '{$lastContentUid}' ");
		$lastContentUserInfo = current($lastContentUserInfo);
		$this->assign('lastContentUserInfo', $lastContentUserInfo);
		$this->assign('lastContent', $lastContent);
//		查询用户信息
		$searchUserInfo = $this->searchUserInfo($uid);
		$this->assign('searchUserInfo', $searchUserInfo);
		//用户采纳率
//		var_dump($searchUserInfo['answer']);die;
		if($searchUserInfo['answer']<1){
			$probability=0;
		}else{
			$probability = $searchUserInfo['accept']/$searchUserInfo['answer']*0.01;
			$probability =round($probability,2);
		}
		
		$probability .='%';
		 $this->assign('probability',$probability);
		 
		 $this->randContent();
		
		$this->display();
		
	}
	/*
	 * 显示内容查询
	 */
	private function content(){
		$content=M('hd_category')->query("SELECT * FROM hd_category WHERE cid={$cid} ");
	}
	/*
	 * 回答问题
	 */
	public function answer(){
		//获取内容  获取用户id  用户提回答加1 用户积分加1 记录时间    asid 
		$this->postcheck();
		if(IS_POST){		
				$postTime = time();
				$asid = $_POST['asid']; //栏目id					
				$uid = $_SESSION['uid']; //用户id
			$content =strip_tags($_POST['content']);
			$content = str_replace(array('\'','\"',',',';'),'',$content); 
			//插入数据
			M('hd_ask')->exec("INSERT INTO hd_answer (content,time,uid,asid) values('$content','$postTime','$uid','$asid')");
//			查询用户信息
			$answer = M('hd_ask')->query("SELECT * FROM hd_user WHERE uid='{$uid}'");
			//用户回答数+1
			$answerNUM = $answer[0]['answer']+1;
			//用户经验+1
			$point =$answer[0]['point']+C('answer');
			//用户金币+1
			$exp = $answer[0]['exp']+C('answer');
			//修改用户经验信息
			M('hd_user')->exec("UPDATE hd_user SET answer ='{$answerNUM}',point='{$point}',exp='{$exp}' WHERE uid={$uid}");
//			Ask表回答数加1
			$askInfo = M('hd_user')->query("SELECT * FROM  hd_ask WHERE asid ='{$asid}' ");
			$answer = $askInfo[0]['answer']+1;
			M('hd_user')->exec("UPDATE hd_ask SET answer ='{$answerNUM}' WHERE asid ='{$asid}'");
			$this->success('回答成功', './');
		}
	}
	/*
	 * 采纳
	 */
	 public function adopt(){
	 	if(IS_POST){
	 		//接受发布的信息 回答列表id  修改回答列表id 下的解决状态  
			if(!empty($_POST['uid']) || !empty($_POST['anid']) || !empty($_POST['asid']) ){
				$uid = $_POST['uid'];
	 			$anid = $_POST['anid'];
	 			$asid = $_POST['asid'];
	 			//修改回答表状态
				M('hd_answer')->exec("UPDATE hd_answer SET accept='1' WHERE anid='{$anid}'");
				//修改问题表状态
				M('hd_ask')->exec("UPDATE hd_ask SET solve='1' WHERE asid='{$asid}'");
				//修改用户信息
				$userInfo = M('hd_user')->query("SELECT * FROM hd_user WHERE uid='{$uid}'");
				$accept=$userInfo[0]['accept'] + 1;//采纳数
				$point=$userInfo[0]['point']  + C('accept');//金币
				$exp=$userInfo[0]['exp']  + C('accept');//经验
				M('hd_user')->exec("UPDATE hd_user SET accept='{$accept}',point='{$point}',exp='{$exp}' WHERE uid='{$uid}'");
				echo 1;
			}else{
				echo '0';				
			}
			
	 		
	 	}
	 }
	 /*
	  * 底部随即显示为结局问题
	  */
	public function randContent(){
		$randContent = M('hd_ask')->query("SELECT * FROM hd_ask where solve=0 LIMIT 5");
		return $this->assign('randContent',$randContent);
	}
}
	